<template>
	<view class="customer-service">
		<!-- 聊天头部 -->
		<view class="chat-header">
			<view class="header-left">
				<image class="ai-avatar" :src="aiAvatar" mode="aspectFill"></image>
				<view class="header-info">
					<text class="title">AI智能助手</text>
					<view class="service-status">
						<view class="status-dot"></view>
						<text>在线服务中</text>
					</view>
				</view>
			</view>
			<view class="header-right">
				<text class="service-time">服务时间: 24小时</text>
			</view>
		</view>

		<!-- 聊天内容区 -->
		<scroll-view 
			class="chat-container" 
			scroll-y 
			:scroll-top="scrollTop"
			:scroll-with-animation="true"
			@scrolltoupper="onScrollToUpper"
			@scrolltolower="onScrollToLower"
		>
			<view class="chat-messages">
				<!-- 欢迎消息 -->
				<view class="message system">
					<view class="message-content">
						<view class="welcome-title">🎁 欢迎来到盲盒商城！</view>
						<view class="welcome-text">我是您的专属超玩客服助手，很高兴为您服务。</view>
						<view class="welcome-tips">
							<view class="tip-item">• 为您介绍盲盒产品详情和玩法</view>
							<view class="tip-item">• 解答抽盒概率和保底机制</view>
							<view class="tip-item">• 提供收藏和交易建议</view>
							<view class="tip-item">• 处理订单和售后问题</view>
						</view>
						<view class="welcome-end">请问有什么可以帮您？</view>
					</view>
				</view>

				<!-- 消息列表 -->
				<view v-for="(message, index) in messages" 
						:key="index" 
						:class="['message', message.type]">
					<image class="avatar" :src="message.type === 'user' ? userAvatar : aiAvatar" :mode="aspectFill"></image>
					<view class="message-wrapper">
						<view class="message-content">
							<text user-select>{{ message.content }}</text>
						</view>
						<view class="message-time">{{ message.time }}</view>
					</view>
				</view>

				<!-- 加载状态 -->
				<view v-if="isLoading" class="message ai">
					<image class="avatar" :src="aiAvatar" mode="aspectFill"></image>
					<view class="message-wrapper">
						<view class="message-content loading">
							<view class="loading-dots">
								<view class="dot"></view>
								<view class="dot"></view>
								<view class="dot"></view>
							</view>
						</view>
					</view>
				</view>
			</view>
		</scroll-view>

		<!-- 输入区域 -->
		<view class="chat-input">
			<view class="input-wrapper">
				<textarea
					v-model="inputMessage"
					class="input-area"
					:disabled="isLoading"
					:placeholder="isLoading ? '正在等待回复...' : '请输入您的问题...'"
					:maxlength="-1"
					:auto-height="true"
					@confirm="sendMessage"
				></textarea>
				<button 
					class="send-btn"
					:class="{ 'disabled': !inputMessage.trim() || isLoading }"
					@tap="sendMessage"
					:disabled="!inputMessage.trim() || isLoading"
				>
					<text class="btn-text">{{ isLoading ? '发送中...' : '发送' }}</text>
					<view v-if="isLoading" class="btn-loading"></view>
				</button>
			</view>
		</view>
	</view>
</template>

<script>
import { getAICustomerService } from '@/api/customerService.js'

export default {
	data() {
		return {
			messages: [],
			inputMessage: '',
			isLoading: false,
			scrollTop: 0,
			userAvatar: '/static/images/user-avatar.png',
			aiAvatar: '/static/images/ai-avatar.png'
		}
	},
	methods: {
		// 获取当前时间
		getCurrentTime() {
			const now = new Date()
			return `${now.getHours().toString().padStart(2, '0')}:${now.getMinutes().toString().padStart(2, '0')}`
		},
		
		// 发送消息
		async sendMessage() {
			if (!this.inputMessage.trim() || this.isLoading) return

			const currentTime = this.getCurrentTime()
			// 添加用户消息
			this.messages.push({
				type: 'user',
				content: this.inputMessage,
				time: currentTime
			})

			const userMessage = this.inputMessage
			this.inputMessage = ''
			this.isLoading = true

			try {
				// 创建一个空的AI回复消息
				const aiMessage = {
					type: 'ai',
					content: '',
					time: this.getCurrentTime()
				}
				this.messages.push(aiMessage)

				// 调用AI客服接口
				const res = await getAICustomerService(userMessage)
				
				// 模拟打字效果
				const text = res.generation
				for (let i = 0; i < text.length; i++) {
					aiMessage.content += text[i]
					await new Promise(resolve => setTimeout(resolve, 50)) // 打字速度
					this.$nextTick(() => {
						this.scrollToBottom()
					})
				}
			} catch (error) {
				console.error('AI客服接口调用失败:', error)
				uni.showToast({
					title: '获取回复失败，请稍后重试',
					icon: 'none'
				})
			} finally {
				this.isLoading = false
				this.$nextTick(() => {
					this.scrollToBottom()
				})
			}
		},

		// 滚动到底部
		scrollToBottom() {
			const query = uni.createSelectorQuery().in(this)
			query.select('.chat-messages').boundingClientRect(data => {
				this.scrollTop = data.height
			}).exec()
		},

		// 滚动到顶部
		onScrollToUpper() {
			// 可以在这里添加加载历史消息的逻辑
		},

		// 滚动到底部
		onScrollToLower() {
			this.scrollToBottom()
		}
	},
	onLoad() {
		// 初始化时滚动到底部
		this.$nextTick(() => {
			this.scrollToBottom()
		})
	}
}
</script>

<style lang="scss">
.customer-service {
	height: 100vh;
	display: flex;
	flex-direction: column;
	background: #f5f7fa;
}

.chat-header {
	background: linear-gradient(135deg, #ff6b6b, #ff8e8e);
	padding: 30rpx;
	display: flex;
	justify-content: space-between;
	align-items: center;
	box-shadow: 0 2rpx 10rpx rgba(255, 107, 107, 0.2);

	.header-left {
		display: flex;
		align-items: center;
		gap: 20rpx;

		.ai-avatar {
			width: 80rpx;
			height: 80rpx;
			border-radius: 50%;
			border: 4rpx solid rgba(255, 255, 255, 0.3);
			box-shadow: 0 4rpx 12rpx rgba(255, 107, 107, 0.3);
		}

		.header-info {
			.title {
				font-size: 36rpx;
				color: #fff;
				font-weight: bold;
				margin-bottom: 8rpx;
				display: block;
				text-shadow: 0 2rpx 4rpx rgba(0, 0, 0, 0.1);
			}

			.service-status {
				display: flex;
				align-items: center;
				color: rgba(255, 255, 255, 0.9);
				font-size: 24rpx;

				.status-dot {
					width: 12rpx;
					height: 12rpx;
					background: #67c23a;
					border-radius: 50%;
					margin-right: 8rpx;
					box-shadow: 0 0 8rpx #67c23a;
				}
			}
		}
	}

	.header-right {
		.service-time {
			color: rgba(255, 255, 255, 0.8);
			font-size: 24rpx;
		}
	}
}

.chat-container {
	flex: 1;
	padding: 20rpx;
}

.chat-messages {
	padding-bottom: 20rpx;
}

.message {
	display: flex;
	margin-bottom: 30rpx;
	align-items: flex-start;

	&.system {
		justify-content: center;
		margin-bottom: 40rpx;

		.message-content {
			background: #fff;
			padding: 30rpx;
			border-radius: 16rpx;
			box-shadow: 0 4rpx 16rpx rgba(255, 107, 107, 0.1);
			max-width: 80%;
			border: 2rpx solid rgba(255, 107, 107, 0.1);

			.welcome-title {
				font-size: 32rpx;
				color: #ff6b6b;
				font-weight: bold;
				margin-bottom: 16rpx;
			}

			.welcome-text {
				font-size: 28rpx;
				color: #606266;
				margin-bottom: 24rpx;
			}

			.welcome-tips {
				background: #fff5f5;
				padding: 20rpx;
				border-radius: 12rpx;
				margin-bottom: 24rpx;
				border: 2rpx solid rgba(255, 107, 107, 0.1);

				.tip-item {
					font-size: 26rpx;
					color: #606266;
					margin-bottom: 12rpx;
					display: flex;
					align-items: center;

					&::before {
						content: '🎁';
						margin-right: 8rpx;
					}

					&:last-child {
						margin-bottom: 0;
					}
				}
			}

			.welcome-end {
				font-size: 28rpx;
				color: #ff6b6b;
				font-weight: 500;
			}
		}
	}

	&.user {
		flex-direction: row-reverse;

		.message-wrapper {
			margin-right: 20rpx;
			display: flex;
			flex-direction: column;
			align-items: flex-end;

			.message-content {
				background: #ff6b6b;
				color: #fff;
				border-radius: 20rpx 0 20rpx 20rpx;
				box-shadow: 0 2rpx 8rpx rgba(255, 107, 107, 0.2);
			}

			.message-time {
				font-size: 24rpx;
				color: #909399;
				margin-top: 8rpx;
			}
		}
	}

	&.ai {
		.message-wrapper {
			margin-left: 20rpx;
			display: flex;
			flex-direction: column;

			.message-content {
				background: #fff;
				color: #303133;
				border-radius: 0 20rpx 20rpx 20rpx;
				box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.05);
				border: 2rpx solid rgba(255, 107, 107, 0.1);
			}

			.message-time {
				font-size: 24rpx;
				color: #909399;
				margin-top: 8rpx;
			}
		}
	}
}

.avatar {
	width: 80rpx;
	height: 80rpx;
	border-radius: 50%;
	overflow: hidden;
	border: 2rpx solid #e4e7ed;
}

.message-content {
	max-width: 70%;
	padding: 24rpx 32rpx;
	font-size: 28rpx;
	line-height: 1.6;
	word-break: break-all;

	&.loading {
		padding: 24rpx 32rpx 0;

		.loading-dots {
			display: flex;
			gap: 8rpx;

			.dot {
				width: 16rpx;
				height: 16rpx;
				background: #909399;
				border-radius: 50%;
				animation: bounce 1.4s infinite ease-in-out;

				&:nth-child(1) { animation-delay: -0.3s; }
				&:nth-child(2) { animation-delay: -0.2s; }
			}
		}
	}
}

.chat-input {
	background: #fff;
	padding: 20rpx;
	box-shadow: 0 -2rpx 10rpx rgba(255, 107, 107, 0.1);

	.input-wrapper {
		display: flex;
		gap: 20rpx;
		align-items: flex-end;
	}

	.input-area {
		flex: 1;
		background: #fff5f5;
		border-radius: 12rpx;
		padding: 20rpx;
		font-size: 28rpx;
		min-height: 80rpx;
		max-height: 160rpx;
		border: 2rpx solid rgba(255, 107, 107, 0.1);
	}

	.send-btn {
		width: 160rpx;
		height: 80rpx;
		line-height: 80rpx;
		text-align: center;
		background: #ff6b6b;
		color: #fff;
		border-radius: 12rpx;
		font-size: 28rpx;
		position: relative;
		overflow: hidden;
		box-shadow: 0 4rpx 12rpx rgba(255, 107, 107, 0.2);

		&.disabled {
			background: #c0c4cc;
		}

		.btn-text {
			position: relative;
			z-index: 1;
		}

		.btn-loading {
			position: absolute;
			top: 0;
			left: 0;
			width: 100%;
			height: 100%;
			background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.2), transparent);
			animation: loading 1.5s infinite;
		}
	}
}

@keyframes bounce {
	0%, 80%, 100% { transform: scale(0); }
	40% { transform: scale(1); }
}

@keyframes loading {
	0% { transform: translateX(-100%); }
	100% { transform: translateX(100%); }
}
</style>
